import time
import ddddocr
from DrissionPage import ChromiumOptions, Chromium

option = ChromiumOptions()
# 设置无痕
option.incognito(on_off=True)

tab = Chromium(option).latest_tab


def read_silder(target_bytes, background_bytes):
    det = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)

    res = det.slide_match(target_bytes, background_bytes)

    return res


def drop_slider(slider, distance):
    action = tab.actions
    # 抓住滑块
    action.hold(slider)
    print("拖动滑块")

    # 拖动滑块
    action.move(offset_x=distance, offset_y=0, duration=0.5)


    tab.wait(0.5)
    action.release()
    print("拖动完成")


def init_page():
    # 登录
    tab.get("https://sz.jd.com/sz/view/index/login.html?ReturnUrl=http%3A%2F%2Fsz-online.jd.com%2F")
    tab.wait(2)
    login = tab.ele(".login-btn btn")
    if login:
        login.click()
        print("点击登录按钮")
    name = tab.ele("#loginname")
    if name:
        name.input("555666")
        print("输入账号")
    tab.wait(2)
    passwd = tab.ele("#nloginpwd")
    if passwd:
        passwd.input("777888")
        print("输入密码")
    tab.wait(2)
    submit = tab.ele("#loginsubmit")
    if submit:
        submit.click()
        print("点击提交")
    tab.wait(3)


    # 处理验证码
    background_content = tab.ele("#main_img").src()
    slot_content = tab.ele("#slot_img").src()
    result = read_silder(slot_content, background_content)


    distance = result['target'][0] + 2
    print(f"验证码识别结果：{result}，滑动距离{distance}")


    # 拖动滑块
    slider = tab.ele(".slide_path")
    if slider:
        drop_slider(slider, distance)


if __name__ == '__main__':
    init_page()
